﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace VBAConvertor.Parsers
{
    public class OpReplacementParser : ILineParser
    {
        private static readonly Regex Op =
            new Regex(
                @"\bAnd\b|\bOr\b|\bNot\b|(\bIs Nothing\b)|([\w\$]:=)",
                RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Singleline);

        public string Parse(Stack<ParseStatus> statuses, string lineContent, LinesReader reader, out bool success, ref bool lineClosed)
        {
            success = Op.IsMatch(lineContent);
            if (!success) return lineContent;
            return Op.Replace(lineContent, match =>
                {
                    if (match.Success)
                    {
                        switch (match.Value)
                        {
                            case "And":
                                return "&&";
                            case "Or":
                                return "||";
                            case "Not":
                                return "!";
                            case "Is Nothing":
                                return " == null ";
                            default:
                                if (match.Value.EndsWith(":="))
                                {
                                    return match.Value.Substring(0, match.Value.Length - 1);
                                }
                                throw new NotImplementedException();
                        }
                    }
                    throw new Exception();
                });
        }
    }
}
